clear
import delim using "./input/controls/citycontrols_1910_expanded_avg_age_withid.csv"

drop 	check

***********************
** Variable Creation **

** Literacy / Schooling
gen		sh1910_inschool = school2/(school1 + school2)
gen		sh1910_lit = lit4/(lit1 + lit2 + lit3 + lit4)

** German Descent 
gen		sh1910_germandescent = german/population
gen		sh1910_germanlang = german_mtongue/population

** Imputed Age

gen 	sh1910_age020 = age020 / (age020 + age2030 + age3040 + age4050 + age5060 + age6070 + age70plus)
gen 	sh1910_age2030 = age2030 / (age020 + age2030 + age3040 + age4050 + age5060 + age6070 + age70plus)
gen 	sh1910_age3040 = age3040 / (age020 + age2030 + age3040 + age4050 + age5060 + age6070 + age70plus)
gen 	sh1910_age4050 = age4050 / (age020 + age2030 + age3040 + age4050 + age5060 + age6070 + age70plus)
gen 	sh1910_age5060 = age5060 / (age020 + age2030 + age3040 + age4050 + age5060 + age6070 + age70plus)
gen 	sh1910_age6070 = age6070 / (age020 + age2030 + age3040 + age4050 + age5060 + age6070 + age70plus)
gen 	sh1910_age70plus = age70plus / (age020 + age2030 + age3040 + age4050 + age5060 + age6070 + age70plus)

gen sh1910_age20to60 = sh1910_age2030 + sh1910_age3040 + sh1910_age4050 + sh1910_age5060

gen ave_age_imputed = 10*sh1910_age020 + 25*sh1910_age2030 + 35*sh1910_age3040 + 45*sh1910_age4050 + 55*sh1910_age5060 + 65*sh1910_age6070 + 75*sh1910_age70plus

sum sh1910*

** Precise Age
rename avg_age age1910_census
drop sum_ages age_denominator

pwcorr sh1910_inschool sh1910_lit
pwcorr sh1910_german*
pwcorr sh1910_inschool sh1910_age020 
pwcorr sh1910_inschool age1910_census
pwcorr sh1910_age20to60 age1910_census


***********************
** Final Cleaning/Saving **

keep 	npi_id extra_id sh1910_inschool sh1910_lit sh1910_germandescent sh1910_germanlang sh1910_age20to60 age1910_census

preserve
	keep if !mi(npi_id)
	save "./output/intermediate/controls_43cities", replace
restore

preserve
	keep if extra_id!=""
	save "./output/intermediate/controls_7cities", replace
restore

clear